var app = angular.module('app');

app.directive('dateTimePicker', [function () {
    return {
        templateUrl: 'tpl/datePicker/dateTimePicker.html',
        restrict: 'EA',
        replace: false,
        scope: {
            maxDate: '=',
            minDate: '=',
            format: '@',
            isRequired: '=',
            ngModel: '=',
            placeHolder: '@'
        },
        link: function (scope, element, attrs) {
            scope.isOpened = false;
            var now = new Date();
            scope.date = now.format('yyyy-MM-dd');
            scope.ngModel = now;
            scope.time = now;
            scope.dateOptions = {
                formatYear: 'yyyy',
                startingDay: 1,
                showWeeks: false,
                autoclose: true
            };
            scope.openDate = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();

                scope.isOpened = true;
            };
            scope.dateChanged = function () {
                scope.ngModel.setFullYear(scope.date.getFullYear());
                scope.ngModel.setMonth(scope.date.getMonth());
                scope.ngModel.setDate(scope.date.getDate());
            };
            scope.timeChanged = function () {
                scope.ngModel.setHours(scope.time.getHours());
                scope.ngModel.setMinutes(scope.time.getMinutes());
            };
        }
    };
}]);